#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: muchen
@file: conftest.py
@time: 2024/12/25
"""
import time

import allure
import pytest
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from autoTest_ecshop.utils.file_utils import open_yaml
from autoTest_ecshop.pages.loginPage import LoginPage

url = open_yaml("common.yaml").get("url")
user = open_yaml("common.yaml").get("user")
pwd = open_yaml("common.yaml").get("pwd")


@pytest.fixture(scope='session')
def driver():
    global driver
    # 实例化浏览器
    driver = webdriver.Edge()
    driver.get(url)
    login = LoginPage(driver)

    # 登录
    login.input_text(login.username_loc, user)
    login.input_text(login.password_loc, pwd)
    login.click(login.login_button_loc)
    time.sleep(0.5)

    print("我是前置步骤 -> 打开浏览器并进入首页")
    # 返回浏览器
    yield driver
    print("我是后置步骤 -> 关闭浏览器")
    # 关闭浏览器
    driver.quit()


# @pytest.hookimpl(hookwrapper=True, tryfirst=True)
# def pytest_runtest_makereport(item, call):
#     # 获取钩子函数的结果
#     out = yield
#     # 获取测试的报告
#     report = out.get_result()
#     # 脚本执行失败，截图
#     if report.when == 'call' and report.outcome == "failed":
#         # 把截图保存到本地
#         driver.save_screenshot("./pic/test.png")
#         # 截图 get_screenshot_as_png()，二进制数据
#         # 使用 allure.attach()，将二进制数据截图附加到allure报告中
#         allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
